
package com.zcsy.manage.web.mall;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zcsy.commons.constants.Constants;
import com.zcsy.commons.constants.DistributionConstants;
import com.zcsy.commons.model.RequestConstants;
import com.zcsy.commons.model.RequestJson;
import com.zcsy.commons.unique.UniqueNumberUtils;
import com.zcsy.commons.util.DateUtils;
import com.zcsy.manage.model.base.CellInfo;
import com.zcsy.manage.model.base.CommonConfig;
import com.zcsy.manage.model.base.CommonImg;
import com.zcsy.manage.model.base.ShopInfo;
import com.zcsy.manage.model.complain.ComplaintsInfo;
import com.zcsy.manage.model.mall.GoodsComment;
import com.zcsy.manage.model.mall.GoodsInfo;
import com.zcsy.manage.model.mall.IntegralConfig;
import com.zcsy.manage.model.mall.IntegralRecord;
import com.zcsy.manage.model.mall.OrderDetailInfo;
import com.zcsy.manage.model.mall.OrderInfo;
import com.zcsy.manage.model.system.User;
import com.zcsy.manage.persistence.weixin.StringUtil;
import com.zcsy.manage.service.base.ICommonConfigService;
import com.zcsy.manage.service.base.ICommonImgService;
import com.zcsy.manage.service.base.IShopInfoService;
import com.zcsy.manage.service.complain.IComplaintsInfoService;
import com.zcsy.manage.service.mall.IGoodsCommentService;
import com.zcsy.manage.service.mall.IGoodsInfoService;
import com.zcsy.manage.service.mall.IIntegralConfigService;
import com.zcsy.manage.service.mall.IIntegralRecordService;
import com.zcsy.manage.service.mall.IOrderInfoService;
import com.zcsy.manage.service.mall.IOwnerAddressService;
import com.zcsy.manage.service.system.IUserService;

/**
 * @Title: OrderController.java
 * @Package： com.zcsy.manage.web.mall 
 * @Description: 订单控制层
 * @author huangls 1449700087@qq.com
 * @date 2016年8月1日
 * @version 1.0
 * @see JDK1.7.0_75
 * @Copyright: 2016 武汉恒数利通技术有限公司 All rights reserved.
 */
@Controller
@RequestMapping(value = "orderInfo")
public class OrderController {

	@Autowired
	private IOrderInfoService orderInfoService;
	
	@Autowired
	private IGoodsInfoService goodsInfoService;
	
	@Autowired
	private IGoodsCommentService goodsCommentService;
	
	@Autowired
    private IOwnerAddressService ownerAddressService;
	
	@Autowired
    private IUserService userService;
	
	@Autowired
    private IShopInfoService shopInfoService;
	
	@Autowired
    private IComplaintsInfoService infoService;
	
	@Autowired
    private ICommonImgService commonImgService;
	
	@Autowired
    private ICommonConfigService commonConfigService;
	
	@Autowired
	private IIntegralRecordService integralRecordService;
	
	@Autowired
	private IIntegralConfigService IntegralConfigService;
	
    /**
     * @Description:根据订单状态，用户当前小区，用户登录名查询订单，返回订单列表，待付款，待收货，待发货，待商家确认，退款售后
     * @param pNo 页数
     * @param states 订单状态字符串
     * @return 订单列表，待付款，待收货，待发货，待商家确认，退款售后
     * @author huangls 1449700087@qq.com
     * @date 2016年8月4日
     */
    @RequestMapping(value="orderInfoListByState" , method = RequestMethod.GET)
    public String getOrderInfoListByState(Model model, Integer pNo, String states, HttpServletRequest req){
    	
    	//获取到当前用户登陆时默认的小区id
        String cellId = ((CellInfo) req.getSession().getAttribute(RequestConstants.OWNER_CELL_INFO)).getId();
		
		//获取当前登陆的用户的信息
        User user = (User)req.getSession().getAttribute(RequestConstants.USER_SESSION_ID);
		String userAccount = user.getLoginname();
    	
		// 业主当前小区的所有订单信息，以及订单详情信息和订单详情下的商品信息
		List<OrderInfo> orderInfoListAll = orderInfoService.selectOrderInfoByCellIdStateUserAccount(cellId,userAccount, states);
		
	    if(pNo == null){
            pNo = Constants.PAGE_NO;
        }
	    
        //此处添加PageHelper将会走拦截器，进行分页，分页方法只针对紧跟着PageHelper的查询方法有效
        PageHelper.startPage(pNo, Constants.PAGE_SIZE);
        
        //仅查询订单信息 ，用于分页
        List<OrderInfo> orderInfoList = null;
        if(Constants.ALL_ORDER.equals(states) || StringUtil.isEmptyString(states)) {
            orderInfoList = orderInfoService.selectOrderAllInfoList(cellId, userAccount);
        } else {
            orderInfoList = orderInfoService.selectOrderInfoList(cellId, userAccount, states);
        }
        for( int i = 0; i < orderInfoList.size(); i++) {
        	for(int j = 0; j < orderInfoListAll.size(); j++){
        	    if(Constants.ORDER_LEVEL_ONE == orderInfoList.get(i).getOrderLevel()) {
                    if(orderInfoList.get(i).getId().equals(orderInfoListAll.get(j).getParentId())) {
                        
                        if(null == orderInfoList.get(i).getOrderDetailList()) {
                            orderInfoList.get(i).setOrderDetailList(orderInfoListAll.get(j).getOrderDetailList());
                        } else {
                            for(int k = 0; k < orderInfoListAll.get(j).getOrderDetailList().size(); k++) {
                                orderInfoList.get(i).getOrderDetailList().add(orderInfoListAll.get(j).getOrderDetailList().get(k));
                            }
                        }
                    }
                } else {
                    //将orderInfoListAll对应订单的订单详情信息插入到orderInfoList中
                    if(orderInfoList.get(i).getId().equals(orderInfoListAll.get(j).getId())) {
                        if(null == orderInfoList.get(i).getOrderDetailList()) {
                            orderInfoList.get(i).setOrderDetailList(orderInfoListAll.get(j).getOrderDetailList());
                        } else {
                            
                            for(int k = 0; k < orderInfoListAll.get(j).getOrderDetailList().size(); k++) {
                                orderInfoList.get(i).getOrderDetailList().add(orderInfoListAll.get(j).getOrderDetailList().get(k));
                            }
                        }
                    }
                }
        	}
        	
        }
        
        PageInfo<OrderInfo> info = new PageInfo<OrderInfo>(orderInfoList);
		model.addAttribute("orderInfos", info);
		model.addAttribute("currentPage", pNo);
		model.addAttribute("userAccount", userAccount);
		model.addAttribute("states", states);
		
    	return "mall/order/orderList";
    }
    
    /**
     * @Description:订单详情
     * @param orderInfoId 订单Id
     * @author huangls 1449700087@qq.com
     * @date 2016年8月4日
     */
    @RequestMapping(value="orderDetail" , method = RequestMethod.GET)
    public String getOrderDetail(Model model, String orderInfoId, HttpServletRequest req) {
    	
    	if(orderInfoId != null) {
    		
	    	OrderInfo orderInfo = orderInfoService.selectOrderInfoAllById(orderInfoId);
	    	
	    	//母订单
	    	if(orderInfo.getOrderLevel() == 1){
	    		
	    		//查询母订单下的所有子订单
	    		List<OrderInfo> kidOrderList = orderInfoService.selectOrderByParentId(orderInfoId);
	    		
	    		OrderInfo order = null;
	    		for(int i = 0; i < kidOrderList.size() ; ++i) {
	    			
	    			//查询有订单详情的子订单
	    			order = orderInfoService.selectOrderInfoAllById(kidOrderList.get(i).getId());
	    			
	    			//将对应的订单详情放入到母订单中
	    			for(int j = 0; j < order.getOrderDetailList().size(); ++j){
	    				orderInfo.getOrderDetailList().add(order.getOrderDetailList().get(j));
	    			}
	    		}
	    	}
	    	
	    	if(orderInfo.getSenderAccount() != null) {
		    	//获取抢单人信息
		    	User user = userService.selectByLoginName(orderInfo.getSenderAccount());
		    	model.addAttribute("senderAccountPhone", user.getPhone());
		    	model.addAttribute("senderName", user.getUsername());
	    	}
	    	
	    	model.addAttribute("orderInfo", orderInfo);
	    	model.addAttribute("orderInfoId", orderInfoId);
	    	
    	}
		
    	return "mall/order/orderDetail";
    }
    
    /**
     * @Description:申请退款，申请退货
     * @param orderInfoId  订单Id
     * @author huangls 1449700087@qq.com
     * @date 2016年8月5日
     */
    @RequestMapping(value="applyRefundOrGoods" , method = RequestMethod.GET)
    public String applyRefundOrGoods(Model model, String orderInfoId, HttpServletRequest req){
    	
    	if(orderInfoId != null) {
	    	OrderInfo orderInfo = orderInfoService.selectOrderInfoAllById(orderInfoId);
	    	model.addAttribute("orderInfo", orderInfo);
	    	model.addAttribute("orderInfoId", orderInfoId);
    	}
		
    	return "mall/order/refundAfter";
    }
    
    /**
     * @Description:提交退款退货申请
     * @param orderInfoId 订单Id
     * @param state 订单状态
     * @param applyReason 退款/退货原因
     * @return 订单列表页面
     * @author huangls 1449700087@qq.com
     * @date 2016年8月5日	
     */
    @RequestMapping(value="saveApply" , method = RequestMethod.POST)
    @ResponseBody
    public RequestJson saveRefundAfter(Model model, String orderInfoId, Integer state,  String applyReason, HttpServletRequest req){
    	
    	RequestJson result = new RequestJson();
    	
    	try{
	    	if(orderInfoId != null && state != null) {
	    		
	    		OrderInfo orderInfo = orderInfoService.selectOrderInfoAllById(orderInfoId);
	    		Date applyTime = new Date();
		    	
		    	if(DistributionConstants.WAIT_CONFIRM == state || DistributionConstants.WAIT_DELIVER == state){
		    		orderInfo.setState(DistributionConstants.APPLY_RETURN_MONEY);
		    		orderInfo.setRefundRequestTime(applyTime);
		    	}
		    	
		    	if(DistributionConstants.IS_COMPLETED == state){
		    		orderInfo.setState(DistributionConstants.APPLY_RETURN_GOODS);
		    		orderInfo.setReturnRequestTime(applyTime);
		    	}
		    	
		    	orderInfo.setReason(applyReason);
		    	orderInfoService.update(orderInfo);
	    	}
	    	result.setSuccess(true);
	    	
    	} catch (Exception e) {
            e.printStackTrace();
            result.setSuccess(false);
        }
    	return result;
    }
    
    /**
     * @Description:取消订单
     * @param orderInfoId 订单id
     * @return 订单列表页面
     * @author huangls 1449700087@qq.com
     * @date 2016年8月5日
     */
    @RequestMapping(value="cancelOrder" , method = RequestMethod.POST)
    @ResponseBody
    public RequestJson cancelOrder(Model model, String orderInfoId, HttpServletRequest req){
    	RequestJson result = new RequestJson();
    	try{
    	    orderInfoService.cancelOrder(orderInfoId);
	    	result.setSuccess(true);
    	} catch (Exception e) {
            e.printStackTrace();
            result.setSuccess(false);
        }
    	return result;
    }
    
    /**
     * @Description:商品评价
     * @param goodsId 商品Id
     * @param orderId 订单Id
     * @author huangls 1449700087@qq.com
     * @date 2016年8月4日
     */
    @RequestMapping(value="comment" , method = RequestMethod.GET)
    public String comment(Model model, String goodsId, String orderId, HttpServletRequest req){
    	
    	if(goodsId != null  && orderId != null) {
    		
    		//查询订单信息，及订单对应的多个订单详情信息，及订单详情对应的商品信息
	    	OrderInfo orderInfo = orderInfoService.selectOrderInfoAllById(orderId);
	    	//查询商品信息
	    	GoodsInfo goodsInfo = goodsInfoService.getGoodsInfoByPrimaryKey(goodsId);
	    	
	    	OrderDetailInfo orderDetailInfo = new OrderDetailInfo();
	    	String goodsInfoId = null;
	    	
	    	for(int i = 0; i < orderInfo.getOrderDetailList().size(); i++){
	    		orderDetailInfo = orderInfo.getOrderDetailList().get(i);
	    		goodsInfoId = orderDetailInfo.getGoodsId();
	    		
	    		//在订单的多个订单详情中，取出与当前商品对应的订单详情
	    		if(goodsId.equals(goodsInfoId) ){
	    			model.addAttribute("orderDetailInfo", orderDetailInfo);
	    			break;
	    		}
	    	}
	    	
	    	model.addAttribute("goodsInfo", goodsInfo);
	    	model.addAttribute("orderId", orderId);
    	}
		
    	return "mall/order/comment";
    }
    
    /**
     * @Description:保存商品评价
     * @param goodsId 商品Id
     * @param content 评价内容
     * @param score评价分数
     * @return 订单详情页面
     * @author huangls 1449700087@qq.com
     * @date 2016年8月5日
     */
    @RequestMapping(value="saveComment" , method = RequestMethod.POST)
    @ResponseBody
    public RequestJson saveComment(Model model, String goodsId, String content, Integer score, 
		String specificationId, HttpServletRequest req){
    	
    	RequestJson result = new RequestJson();
    	GoodsComment goodsComment = new GoodsComment();
    	
    	//获取当前登陆的用户的信息
        User user = (User)req.getSession().getAttribute(RequestConstants.USER_SESSION_ID);
    	
    	try{
	    	if(goodsId != null) {
	    		goodsComment.setId(UniqueNumberUtils.get18UniqueNumber());
	    		goodsComment.setGoodsId(goodsId);
	    		goodsComment.setContent(content);
	    		goodsComment.setScore(score);
	    		goodsComment.setUserAccount(user.getLoginname());
	    		goodsComment.setSpecificationId(specificationId);
	    		goodsCommentService.insert(goodsComment);
	    		
	    		/**************************************用户单次评论获得积分5************************************/
	            //用户Id
	            String userId = user.getId();
	            String userAccount = user.getLoginname();
	            //根据唯一标识name获取“每日积分上限”的小区配置信息
	            CommonConfig commonConfig = commonConfigService.getByName("mall_integral_config");
	            //用户当天可加积分之和的上限
	            Integer maxValuePerDay = commonConfigService.getIntegralMaxValueOfCell("mall_integral_config");
	            //根据唯一标识name获取"投诉建议或报修"可获得的积分值
	            Integer integralOfcomplaintAndRepair = IntegralConfigService.getIntegralValue("common");
	            //获取用户当天积分变动之和
	            Integer soFarIntegralSum = integralRecordService.getIntegralOfToday(userId);
	           
	            // 验证小区配置的积分上限值不为空以及值得有效性
	            if (commonConfig != null && maxValuePerDay.toString().matches("[0-9]+")) {
	                // 当日积分上限 > 投诉建议获得积分 && 当天的积分变动值之和 <（积分上限-投诉建议获得积分）
	                if (maxValuePerDay > integralOfcomplaintAndRepair && soFarIntegralSum < (maxValuePerDay - integralOfcomplaintAndRepair)) {
	                    //插入积分变更信息
	                    integralRecordService.changeIntegralMethod(userAccount, "common");
	                    result.setSuccess(true);
	                    result.setMsg("评价成功获得5个积分！");
	                }
	                // 用户当日变动的积值之和 == 积分上限
	                if(soFarIntegralSum == maxValuePerDay) {
	                    result.setMsg("已达今日累加积分上限");
	                }
	                // 用户当日变动积分值之和 < 积分上限 && 用户当日变动积分值之和 >(积分上限-投诉建议成功获得的积分)
	                if(soFarIntegralSum < maxValuePerDay && soFarIntegralSum > (maxValuePerDay - integralOfcomplaintAndRepair)) {
	                    // 插入积分变更信息
	                    integralRecordService.changeIntegralWhenToMaxValuePerDay(userAccount, "common",maxValuePerDay - soFarIntegralSum);
	                    result.setMsg("评价成功获得5个积分！，但是已达今日累积积分上限！");
	                }
	            }
		    	result.setSuccess(true);
	    	}
    	} catch (Exception e) {
            e.printStackTrace();
            result.setSuccess(false);
        }
    	return result;
    } 
    
    /**
     * @Description:确认收货
     * @param orderInfoId 订单id
     * @return 订单列表页面
     * @author huangls 1449700087@qq.com
     * @date 2016年8月8日
     */
    @RequestMapping(value="confirmReceiveGoods" , method = RequestMethod.POST)
    @ResponseBody
    public RequestJson confirmReceiveGoods(Model model, String orderInfoId, HttpServletRequest req){
    	
    	RequestJson result = new RequestJson();
    	OrderInfo orderInfo = orderInfoService.selectByPrimaryId(orderInfoId);
    	
    	//业主确认收货时间
    	Date confirmTime = new Date();
    	
    	try{
	    	if(orderInfoId != null) {
	    		orderInfo.setState(DistributionConstants.IS_COMPLETED);
	    		orderInfo.setConfirmTime(confirmTime);
		    	orderInfoService.update(orderInfo);
		    	result.setSuccess(true);
	    	}
    	} catch (Exception e) {
            e.printStackTrace();
            result.setSuccess(false);
        }
    	return result;
    }
    
    /**
     * @Description:立即付款跳转界面
     * @param orderInfoId 订单Id
     * @author huangls 1449700087@qq.com
     * @date 2016年8月11日
     */
    @RequestMapping(value="payMoney", method = RequestMethod.GET)
    public String payMoney(Model model,String orderInfoId, HttpServletRequest request) {

        OrderInfo orderInfo = orderInfoService.selectByPrimaryId(orderInfoId);
        model.addAttribute("orderInfo", orderInfo);
        
        return "mall/order/payMoney";
    }
    
    /**
     * @Description: 确认支付
     * @param orderInfoId 订单id
     * @author huangls 1449700087@qq.com
     * @date 2016年8月11日
     */
    @RequestMapping(value="comfirmPayMoney" , method = RequestMethod.POST)
    @ResponseBody
    public RequestJson comfirmPayMoney(Model model, String orderInfoId, HttpServletRequest req){
    	
    	RequestJson result = new RequestJson();
    	OrderInfo orderInfo = orderInfoService.selectByPrimaryId(orderInfoId);
    	
    	//业主支付时间
    	Date payTime = new Date();
    	
    	try{
	    	if(orderInfoId != null) {
	    		orderInfo.setState(DistributionConstants.IS_COMPLETED);
	    		orderInfo.setPayTime(payTime);
		    	orderInfoService.update(orderInfo);
		    	result.setSuccess(true);
	    	}
    	} catch (Exception e) {
            e.printStackTrace();
            result.setSuccess(false);
        }
    	return result;
    }
    
    /**
     * @Description: 携带商铺id进入投诉建议页面
     * @param shopId 商铺id
     * @return 投诉建议页面
     * @author huangls 1449700087@qq.com
     * @date 2016年8月18日
     */
    @RequestMapping(value="complainSeller", method = RequestMethod.GET)
    public String complainSeller(Model model,String shopId, HttpServletRequest request) {
    	
//    	if(shopId != null && shopId != "") {
	        ShopInfo shopInfo = shopInfoService.selectByPrimaryKey(shopId);
	        model.addAttribute("shopInfo", shopInfo);
//    	}
	        return "mall/order/complainSeller";
    }
    
    /**
     * @description 保存投诉建议以及业主的回复信息
     * @param complaints
     *            投诉对象
     * @return RequestJson 成功或失败
     * @author chenxin
     * @date 2016年7月29日下午3:23:33
     */
    @RequestMapping(value = "saveComplain", method = RequestMethod.POST)
    @ResponseBody
    public RequestJson saveComplain(Model model, String title, String content, Integer suggestionType,
            Integer complainType, String objId, String contactor, String contactorPhone, Integer level, Integer type,
            @RequestParam(value = "imgArr[]") String[] imgArr, HttpServletRequest req) {

        RequestJson result = new RequestJson();

        try {
            User user = (User) req.getSession().getAttribute(RequestConstants.USER_SESSION_ID);
            ComplaintsInfo complaints = new ComplaintsInfo();
            complaints.setOwnerId(user.getId());
            complaints.setId(UniqueNumberUtils.get18UniqueNumber());
            complaints.setCreateTime(DateUtils.getDate());
            complaints.setTitle(title);
            complaints.setComplainType(complainType);
            complaints.setContactorPhone(contactorPhone);
            complaints.setContactor(contactor);
            complaints.setContent(content);
            complaints.setLevel(level);
            complaints.setObjectId(objId);
            complaints.setType(type);
            complaints.setImgArr(imgArr);
            complaints.setSugesstionType(suggestionType);
            complaints.setState(Constants.STATE_DISABLED);

            infoService.insert(complaints);

            List<CommonImg> imgs = commonImgService.selectImgsByObjIdAndObjType(complaints.getId(),
                    Constants.IMG_COMPLAIN);

            if (CollectionUtils.isNotEmpty(imgs)) {
                for (CommonImg commonImg : imgs) {
                    commonImgService.deleteById(commonImg.getId());
                }
            }
            String[] imgArrs = complaints.getImgArr();
            CommonImg commonImg = new CommonImg();
            for (String item : imgArrs) {
                commonImg.setId(UniqueNumberUtils.get18UniqueNumber());
                commonImg.setUrl(item);
                commonImg.setObjId(complaints.getId());
                commonImg.setObjType(Constants.IMG_COMPLAIN);
                commonImgService.insert(commonImg);
            }
            result.setSuccess(true);
            result.setMsg("操作成功！");
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMsg("操作异常！");
        }
        return result;
    }
    
    /**
     * @Description:个人中心首页签到得积分
     * @param userId 当前用户id
     * @return 个人中心首页
     * @author huangls 1449700087@qq.com
     * @date 2016年8月26日
     */
    @RequestMapping(value="signIn" , method = RequestMethod.POST)
    @ResponseBody
    public RequestJson SignIn(Model model, String userId,  HttpServletRequest req){
    	
    	RequestJson result = new RequestJson();
    	
    	try{
    		//获取当前登陆的用户的信息
            User user = (User)req.getSession().getAttribute(RequestConstants.USER_SESSION_ID);
            User userInfo = userService.selectByLoginName(user.getLoginname());
	        
	        //根据唯一标识name获取小区配置信息
	        CommonConfig commonConfig = commonConfigService.getByName("mall_integral_config");
	        
	       //根据唯一标识name获取积分配置信息
	        IntegralConfig integralConfig = IntegralConfigService.selectByName("Sign");
	        
	        //签到可获得的积分
	        Integer integralSignIn = Integer.valueOf(integralConfig.getValue());
	        
	        //获取当前用户当天的积分变更记录
	        List<IntegralRecord> integralRecordList = integralRecordService.selectRecordByUserAccount(userInfo.getLoginname());
	        
	        //当前用户当天积分变动之和
	        Integer soFarIntegralSum = 0;
	        
	        //查询用户当天签到拿积分的积分变更记录
	        IntegralRecord signInRecord = integralRecordService.selectTodaySignInRecordByUserAccount(userInfo.getLoginname());
	        
	        IntegralRecord integralRecord = new IntegralRecord();
	        
	        if( signInRecord == null) {
	        
		        //求当前用户当天积分变动之和
		        for (int i = 0 ; i < integralRecordList.size() ; i++ ) {
		        	soFarIntegralSum += integralRecordList.get(i).getChange();
		        }
		        
		        if ( commonConfig != null && commonConfig.getValue().matches("[0-9]+")) {
			        
			        //用户当天可加积分之和的上限
			        Integer maxValuePerDay =  Integer.valueOf(commonConfig.getValue());
			        
			        if( maxValuePerDay > integralSignIn && soFarIntegralSum < (maxValuePerDay - integralSignIn) ){
			        	
			        	userInfo.setIntegral(userInfo.getIntegral() + integralSignIn);
			        	userService.updateByPrimaryKeySelective(userInfo);
			        	
			        	//插入积分变更信息
			        	integralRecord.setId(UniqueNumberUtils.get18UniqueNumber());
			        	integralRecord.setChange(integralSignIn);
			        	integralRecord.setRecordTime(new Date());
			        	integralRecord.setUserAccount(userInfo.getLoginname());
			        	integralRecord.setDescription("打卡拿积分");
			        	integralRecordService.insert(integralRecord);
			        	
			        	result.setMsg("签到成功");
			        } 
			        
			        if (soFarIntegralSum == maxValuePerDay) {
			        	result.setMsg("已达今日累加积分上限");
			        } 
			        
			        if (soFarIntegralSum < maxValuePerDay && soFarIntegralSum > (maxValuePerDay - integralSignIn)) {
			        	
			        	userInfo.setIntegral(userInfo.getIntegral() + maxValuePerDay - soFarIntegralSum);
			        	userService.updateByPrimaryKeySelective(userInfo);
			        	
			        	//插入积分变更信息
			        	integralRecord.setId(UniqueNumberUtils.get18UniqueNumber());
			        	integralRecord.setChange(maxValuePerDay - soFarIntegralSum);
			        	integralRecord.setRecordTime(new Date());
			        	integralRecord.setUserAccount(userInfo.getLoginname());
			        	integralRecord.setDescription("打卡拿积分");
			        	integralRecordService.insert(integralRecord);
			        	
			        	result.setMsg("签到成功，达到今日累加积分上限");
			        }
		        } else {
		        	result.setMsg("签到失败");
		        }
		        
	        } else {
	        	result.setMsg("今日已签到");
	        }
	        
	        result.setSuccess(true);
	    	
    	} catch (Exception e) {
            e.printStackTrace();
            result.setSuccess(false);
            result.setMsg(RequestConstants.NET_ERROR);
        }
    	return result;
    } 
    
}


